package com.example.auth.entity;

import java.io.Serializable;


import com.example.auth.Common.ExceptionCodeEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;

/**
 * 响应结果
 * 
 * @author xingmaosheng
 * @since 2019/10/19 22:42
 */
@ApiModel(value = "响应结果")
@Data
@Accessors(chain = true)
public class CommonResult<T> implements Serializable {

	private static final long serialVersionUID = 6081990562580491613L;
	/**
	 * 响应编码
	 */
	@ApiModelProperty(value = "响应编码：0-请求处理成功", example = "0")
	private int code;

	/**
	 * 提示消息
	 */
	@ApiModelProperty(value = "提示消息", example = "操作成功")
	private String msg;

	/**
	 * 响应数据
	 */
	@ApiModelProperty(value = "响应数据")
	private T data;

	public CommonResult() {
	}

	public CommonResult(int code, String message) {
		this.code = code;
		this.msg = message;
	}

	public CommonResult(int code, String message, T data) {
		this.code = code;
		this.msg = message;
		this.data = data;
	}

	// --------------------请求成功返回-----------------------

	public static <T> CommonResult<T> success() {
		return new CommonResult<>(ExceptionCodeEnum.SUCCESS.getCode(), ExceptionCodeEnum.SUCCESS.getMessage());
	}

	public static <T> CommonResult<T> success(T data) {
		CommonResult<T> result = new CommonResult<>(ExceptionCodeEnum.SUCCESS.getCode(), ExceptionCodeEnum.SUCCESS.getMessage());
		result.setData(data);
		return result;
	}

	public static <T> CommonResult<T> success(String msg) {
		return new CommonResult<>(ExceptionCodeEnum.SUCCESS.getCode(), msg);
	}

	public static <T> CommonResult<T> success(String msg, T data) {
		return new CommonResult<>(ExceptionCodeEnum.SUCCESS.getCode(), msg, data);
	}

	// --------------------请求失败返回-----------------------

	public static <T> CommonResult<T> fail() {
		return new CommonResult<>(ExceptionCodeEnum.FAIL.getCode(), ExceptionCodeEnum.FAIL.getMessage());
	}

	public static <T> CommonResult<T> fail(String msg) {
		return new CommonResult<>(ExceptionCodeEnum.FAIL.getCode(), msg);
	}

	public static <T> CommonResult<T> fail(int code, String msg) {
		return new CommonResult<>(code, msg);
	}

	public static <T> CommonResult<T> feignFail() {
		return new CommonResult<>(ExceptionCodeEnum.FEIGN_EXCEPTION.getCode(), ExceptionCodeEnum.FEIGN_EXCEPTION.getMessage());
	}
}